package hn.cch.servlet.configuration;

import hn.cch.servlet.annotation.AccessLimited;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

@Component
public class HandlerMethodInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(HandlerMethodInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;

            AccessLimited accessLimited = handlerMethod.getMethodAnnotation(AccessLimited.class);
            if (accessLimited == null) {
                return super.preHandle(request, response, handler);
            }
            logger.info("interface access limited");

            // response.setContentType(MediaType.APPLICATION_JSON.toString());

        }

        return super.preHandle(request, response, handler);
    }
}
